package com.hg.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hg.mapper.HgMallDishMapper;
import com.hg.pojo.HgMallDish;
import com.hg.pojo.HgMallShop;
import com.hg.pojo.dto.HgMallDishDTO;
import com.hg.service.HgMallDishService;
import com.hg.service.HgMallShopService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-02
 */
@Service
public class HgMallDishServiceImpl extends ServiceImpl<HgMallDishMapper, HgMallDish> implements HgMallDishService {

    @Autowired
    private HgMallShopService hgMallShopService;
    @Transactional
    @Override
    public Page<HgMallDishDTO> dishPage(int currentPage, int pageSize, String dishName, String shopName, String category) {
        Page<HgMallDish> pageInfo = new Page<>(currentPage, pageSize);
        Page<HgMallDishDTO> hgMallDishDTOPage = new Page<>();
        LambdaQueryWrapper<HgMallDish> lm = new LambdaQueryWrapper<>();
        lm.eq(HgMallDish::getDelFlag, 0);
        lm.like(category != "", HgMallDish::getType, category);
        lm.like(shopName != "", HgMallDish::getShopId, shopName);
        lm.like(dishName != "", HgMallDish::getName, dishName);
        lm.select(
                HgMallDish::getId,
                HgMallDish::getName,
                HgMallDish::getIntroduce,
                HgMallDish::getPrice,
                HgMallDish::getType,
                HgMallDish::getStatus,
                HgMallDish::getShopId,
                HgMallDish::getDelFlag,
                HgMallDish::getSales,
                HgMallDish::getImg);
        this.page(pageInfo,lm);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,hgMallDishDTOPage,"records");

        List<HgMallDish> records = pageInfo.getRecords();

        List<HgMallDishDTO> dishList = records.stream().map((item) -> {
            HgMallDishDTO hgMallDishDTO = new HgMallDishDTO();
            //对象拷贝
            BeanUtils.copyProperties(item,hgMallDishDTO);
            //获取商家id
            Integer shopId = item.getShopId();
            //根据id查询所属店铺
            //条件构造器
            LambdaQueryWrapper<HgMallShop> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(HgMallShop::getId,shopId);
            queryWrapper.select(HgMallShop::getName);
            HgMallShop hgMallShop = hgMallShopService.getOne(queryWrapper);
            //获取店铺名字
            String name = hgMallShop.getName();
            hgMallDishDTO.setShopName(name);
            return hgMallDishDTO;
        }).collect(Collectors.toList());
        //最后给DTO添加上数据
        hgMallDishDTOPage.setRecords(dishList);
        return hgMallDishDTOPage;
    }

    @Override
    public Boolean updateDish(HgMallDish hgMallDish) {
       return this.updateById(hgMallDish);
    }

    @Override
    public Boolean deleteDishById(Long id) {

        LambdaUpdateWrapper<HgMallDish> lmU = new LambdaUpdateWrapper<>();
        lmU.set(HgMallDish::getDelFlag, 1);
        lmU.eq(HgMallDish::getId, id);
        boolean update = this.update(lmU);
        if (update){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean deleteShopByIds(Long[] ids) {
        LambdaUpdateWrapper<HgMallDish> lmU = new LambdaUpdateWrapper<>();
        lmU.set(HgMallDish::getDelFlag, 1);
        lmU.in(HgMallDish::getId, ids);
        return this.update(lmU);
    }

    @Override
    public List<HgMallDish> getDishOptionsByShopId(Integer shopId) {
        LambdaQueryWrapper<HgMallDish> lm =new LambdaQueryWrapper<>();
        lm.select(HgMallDish::getId,HgMallDish::getName);
        lm.eq(HgMallDish::getShopId,shopId);
        lm.eq(HgMallDish::getDelFlag,0);
        List<HgMallDish> list = this.list(lm);
        if (list.size()>0){
            return list;
        }
        return null;
    }
}
